import uuid
from datetime import datetime
from typing import Optional, List

from pydantic import BaseModel, field_validator
from app.schemas.base import BaseSchema


class MedicalRecordBase(BaseModel):
    disease_name: Optional[str] = None
    report_category: Optional[str] = None
    report_name: Optional[str] = None
    report_date: Optional[datetime] = None
    hospital_name: Optional[str] = None

    @field_validator('report_date')
    @classmethod
    def validate_report_date(cls, v):
        if v is not None and v.tzinfo is not None:
            # 如果有时区信息，转换为naive datetime
            return v.replace(tzinfo=None)
        return v


class MedicalRecordCreate(MedicalRecordBase):
    user_id: uuid.UUID
    file_id: uuid.UUID
    extraction_status: str = "pending"
    upload_time: datetime


class MedicalRecordUpdate(MedicalRecordBase):
    extraction_status: Optional[str] = None


class MedicalRecordInDB(MedicalRecordBase):
    id: uuid.UUID
    user_id: uuid.UUID
    file_id: uuid.UUID
    extraction_status: str
    upload_time: datetime
    created_at: datetime
    updated_at: datetime

    model_config = {"from_attributes": True}


class MedicalRecordResponse(BaseSchema):
    """医疗档案详情响应"""
    id: uuid.UUID
    file_id: uuid.UUID
    disease_name: Optional[str] = None
    report_category: Optional[str] = None
    report_name: Optional[str] = None
    report_date: Optional[datetime] = None
    hospital_name: Optional[str] = None
    extraction_status: str
    upload_time: datetime
    created_at: datetime
    # 可选：关联文件信息
    filename: Optional[str] = None
    file_type: Optional[int] = None
    file_path: Optional[str] = None

    model_config = {"from_attributes": True}


class MedicalRecordListItem(BaseSchema):
    """医疗档案列表项"""
    id: uuid.UUID
    file_id: uuid.UUID
    disease_name: Optional[str] = None
    report_category: Optional[str] = None
    report_name: Optional[str] = None
    report_date: Optional[datetime] = None
    hospital_name: Optional[str] = None
    extraction_status: str
    upload_time: datetime
    created_at: datetime
    # 关联文件信息
    filename: Optional[str] = None
    file_type: Optional[int] = None
    file_path: Optional[str] = None

    model_config = {"from_attributes": True}


class MedicalRecordListResponse(BaseModel):
    """医疗档案列表分页响应"""
    items: list[MedicalRecordListItem]
    total: int
    page: int
    page_size: int
    total_pages: int


class MedicalRecordCountResponse(BaseModel):
    """医疗档案数量响应"""
    count: int


class MedicalRecordWithConversations(BaseSchema):
    """医疗档案详情及关联对话"""
    id: uuid.UUID
    file_id: uuid.UUID
    disease_name: Optional[str] = None
    report_category: Optional[str] = None
    report_name: Optional[str] = None
    report_date: Optional[datetime] = None
    hospital_name: Optional[str] = None
    extraction_status: str
    upload_time: datetime
    created_at: datetime
    # 关联文件信息
    filename: Optional[str] = None
    file_type: Optional[int] = None
    file_path: Optional[str] = None
    # 关联的对话ID列表
    conversation_ids: List[uuid.UUID] = []

    model_config = {"from_attributes": True}


class MedicalRecordBatchResponse(BaseSchema):
    """同一批次上传的医疗档案响应"""
    upload_time: datetime
    records: List[MedicalRecordWithConversations]